package netwrix.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SolutionTestNG extends netwrix.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSolutionNAAD() throws Exception {
	  loadHomePage();
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for Active Directory")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naad')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './ad_audit.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for Active Directory")).click();
		oneToOneOther();
	  
	    //oneToOneAutoFill();
	    //driver.findElement(By.xpath("(//a[contains(@href, './ad_audit.html')])[2]")).click();
	    //onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for Exchange")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naex')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './exchange_reporting.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for Exchange")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './exchange_reporting.html')])[2]")).click();
		//onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for File Servers")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nafs')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './file_server_auditing.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for File Servers")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './file_server_auditing.html')])[2]")).click();
		//onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for SharePoint")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nasp')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './sharepoint_reporting.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for SharePoint")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './sharepoint_reporting.html')])[2]")).click();
		//onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for SQL Server")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nasql')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './sql_server_audit_reporting.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for SQL Server")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './sql_server_audit_reporting.html')])[2]")).click();
		//onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for VMware")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=navm')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './vmware_reporting.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for VMware")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './vmware_reporting.html')])[2]")).click();
		//onlineTestDrive();
		
		driver.findElement(By.linkText("Solutions")).click();
		driver.findElement(By.linkText("Netwrix Auditor for Windows Server")).click();
		driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naws')]")).click();
		System.out.println(driver.getTitle());
		System.out.println(driver.getCurrentUrl());
		driver.findElement(By.linkText("Request Quote")).click();
		driver.findElement(By.xpath("(//a[contains(@href, './windows_server_audit.html')])[2]")).click();
		askAQuestion();
		driver.findElement(By.linkText("Netwrix Auditor for Windows Server")).click();
		oneToOneOther();
		//oneToOneAutoFill();
		//driver.findElement(By.xpath("(//a[contains(@href, './windows_server_audit.html')])[2]")).click();
		//onlineTestDrive();
		
	}

		private void oneToOneOther() {
			driver.findElement(By.linkText("One-to-One Demo")).click();
			new Select(driver.findElement(By.id("OneToOneForm_dateTimePref"))).selectByVisibleText("Other");
			driver.findElement(By.id("OneToOneForm_dateTimePrefCust")).clear();
			driver.findElement(By.id("OneToOneForm_dateTimePrefCust")).sendKeys("selenium test");
			driver.findElement(By.name("yt0")).click();
			driver.findElement(By.name("yt0")).click();
			new WebDriverWait(driver, 30).until(ExpectedConditions.textToBePresentInElementLocated(By.id("mbody"), "thank you for requesting"));
		}

		private void onlineTestDrive() {
			driver.findElement(By.linkText("Online TestDrive")).click();
			driver.findElement(By.id("requesttd_btn")).click();
			String winHandleBefore = driver.getWindowHandle(); 
	for (String winHandle : driver.getWindowHandles()) {
			driver.switchTo().window(winHandle);
	}
			driver.close();
			driver.switchTo().window(winHandleBefore);
			quizTestDrive();
		}

		private void askAQuestion() {
			driver.findElement(By.linkText("Ask Question")).click();
			driver.findElement(By.id("AskQuestionForm_questionText")).clear();
			driver.findElement(By.id("AskQuestionForm_questionText")).sendKeys("Selenium test");
			driver.findElement(By.name("yt0")).click();
			driver.findElement(By.name("yt0")).click();
			new WebDriverWait(driver, 30).until(ExpectedConditions.textToBePresentInElementLocated(By.id("mbody"), "We will try"));
		}

		private void oneToOneAutoFill() {
			driver.findElement(By.cssSelector("#yw3 > li > a")).click();
			driver.findElement(By.id("OneToOneForm_firstName")).clear();
			driver.findElement(By.id("OneToOneForm_firstName")).sendKeys("Erik");
			driver.findElement(By.id("OneToOneForm_lastName")).clear();
			driver.findElement(By.id("OneToOneForm_lastName")).sendKeys("Borg");
			new Select(driver.findElement(By.id("OneToOneForm_position"))).selectByVisibleText("IT Director");
			driver.findElement(By.id("OneToOneForm_companyName")).clear();
			driver.findElement(By.id("OneToOneForm_companyName")).sendKeys("Netwrix");
			driver.findElement(By.id("OneToOneForm_email")).clear();
			driver.findElement(By.id("OneToOneForm_email")).sendKeys("erik.borg@netwrix.com");
			new Select(driver.findElement(By.id("OneToOneForm_nwf_country"))).selectByVisibleText("United States");
			new Select(driver.findElement(By.id("OneToOne_nwf_state"))).selectByVisibleText("California - Northern");
			driver.findElement(By.id("OneToOneForm_dateTimePref")).clear();
			driver.findElement(By.id("OneToOneForm_dateTimePref")).sendKeys("Selenium test");
			driver.findElement(By.name("yt0")).click();
			driver.findElement(By.name("yt0")).click();
			new WebDriverWait(driver, 30).until(ExpectedConditions.textToBePresentInElementLocated(By.id("mbody"), "thank you for requesting"));
		
		}

		private void loadHomePage() {
			WebElement signinButton = driver.findElement(By.linkText("Sign In"));
			signinButton.click();
			driver.findElement(By.linkText("Already registered?")).click();
			driver.findElement(By.id("LoginForm_username")).clear();
			driver.findElement(By.id("LoginForm_username")).sendKeys("erik.borg@netwrix.com");
			driver.findElement(By.id("LoginForm_password")).clear();
			driver.findElement(By.id("LoginForm_password")).sendKeys("123456");
			driver.findElement(By.name("yt1")).click();
		}

		private void quizTestDrive() {
			driver.findElement(By.id("answer39929X35X391Out")).click();
			driver.findElement(By.id("answer39929X35X395Out")).click();
			driver.findElement(By.id("answer39929X35X406Out")).click();
			driver.findElement(By.id("answer39929X35X399")).clear();
			driver.findElement(By.id("answer39929X35X399")).sendKeys("Selenium test");
			driver.findElement(By.id("movesubmitbtn")).click();
		}
	

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
